package xyz.hubery.service.impl;


import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import xyz.hubery.pojo.CartGoods;
import xyz.hubery.service.CartService;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@DubboService
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void addCart(Long userId, CartGoods cartGoods) {
        redisTemplate.opsForSet().add("cart:"+cartGoods.getGoodId(),userId);
        // 判断该用户购物车中是否包含该商品
        Object o = redisTemplate.opsForHash().get("cart:" + userId, cartGoods.getGoodId());
        if(o != null) { // 已经添加过，修改数量即可
            CartGoods goods = (CartGoods) o;
            goods.setNum(cartGoods.getNum());
            redisTemplate.opsForHash().put("cart:" + userId, cartGoods.getGoodId(),goods);
            return;
        }
        // 添加
        redisTemplate.opsForHash().put("cart:" + userId, cartGoods.getGoodId(),cartGoods);
    }


    @Override
    public void handleCart(Long userId, Long goodId, Integer num) {
        Object o = redisTemplate.opsForHash().get("cart:" + userId, goodId);
        if(o != null) {
            CartGoods cartGoods = (CartGoods) o;
            cartGoods.setNum(num);
            redisTemplate.opsForHash().put("cart:" + userId, goodId,cartGoods);
        }
    }

    @Override
    public void deleteCartOption(Long userId, Long goodId) {
        redisTemplate.opsForSet().remove("cart:"+goodId,userId);
        redisTemplate.opsForHash().delete("cart:"+userId,goodId);
    }

    @Override
    public List<CartGoods> findCartList(Long userId) {
        List<CartGoods> cartGoodsList = new ArrayList<>();
        BoundHashOperations boundHashOps = redisTemplate.boundHashOps("cart:" + userId);
        Set<Long> goodsIdSet = boundHashOps.keys();
        for (Long goodsId : goodsIdSet) {
            Object o = boundHashOps.get(goodsId);
            if(o != null) {
                cartGoodsList.add((CartGoods) o);
            }
        }
        return cartGoodsList;
    }

    @Override
    public void refreshCartGoods(CartGoods cartGoods) {
        // 获取添加了该商品到购物车的用户列表
        Set<Long> userIdsSet = redisTemplate.opsForSet().members("cart:" + cartGoods.getGoodId());
        if(!CollectionUtils.isEmpty(userIdsSet)) {
            for (Long userId : userIdsSet) {
                CartGoods oldCartGoods = (CartGoods) redisTemplate.opsForHash().get("cart:" + userId, cartGoods.getGoodId());
                cartGoods.setNum(oldCartGoods.getNum());
                redisTemplate.opsForHash().put("cart:"+userId,cartGoods.getGoodId(),cartGoods);
            }
        }
    }

    @Override
    public void deleteCartGoods(Long goodId) {
        Set<Long> userIdsSet = redisTemplate.opsForSet().members("cart:" + goodId);
        if(!CollectionUtils.isEmpty(userIdsSet)) {
            for (Long userId : userIdsSet) {
                redisTemplate.opsForHash().delete("cart:"+userId,goodId);
            }
        }
        redisTemplate.opsForSet().remove("cart:" + goodId);
    }


}
